#! /bin/bash
# 此程序用于版本发布

. ./scripts/shell/common.sh
. ./scripts/shell/golang.sh
. ./scripts/shell/tool.sh


CHANGLOG_PATH=./CHANGELOG

printLong "构建"

. ./scripts/050-构建.sh

printLong "准备发布"

TAG=${VERSION}
VERSION_FILE="./internal/initial/info.go"

printShort "修改版本文件： ${VERSION_FILE}"
sed -i "s/\(ysbuild.Version[ ]*=\) ".*"/\1 \"${TAG}\"/g" ${VERSION_FILE}

printShort "生成 ChangeLog"

${GIT} tag ${TAG}
${GIT_CHGLOG}  -o ${CHANGLOG_PATH}/${TAG}.md ${TAG}
${GIT} tag -d ${TAG}
# 提交更新日志
${GIT} add ${CHANGLOG_PATH}/*
${GIT} add ${VERSION_FILE}
${GIT} commit -m "version: ${TAG}"
${GIT} tag -m ${TAG} ${TAG}

printShort "推送到远端"
REPO=$(${GIT} remote get-url origin)
if [[ "${REPO}" == http* ]]
then
    echo "正在将 tags 推送到远端"
    git push
    git push --tags
fi


if [[ "$?" != 0 ]]; then
    exit 1
fi